<template>
    <el-dialog :title="title" class="addPedestal" width="380px" :visible.sync="isDial">
    <el-form :model="form" size="mini" :rules="rules" ref="ruleForm">
        <el-form-item label="单元名称" prop="unitName" :label-width="formLabelWidth">
          <el-input size="mini"  class="leftBorder" :disabled="disabled" v-model="form.unitName" :maxlength="10" autocomplete="off" style="width:182px"></el-input> 单元
        </el-form-item>
        <el-form-item label="总层数" prop="layer" :label-width="formLabelWidth">
          <el-input size="mini"  class="leftBorder" :maxlength="3" v-model="form.layer" autocomplete="off" style="width:182px"></el-input> 层
        </el-form-item>
        <el-form-item label="梯户数" :label-width="formLabelWidth">
          <el-select size="mini" v-model="form.ladder" placeholder="2" style="width:80px">
            <el-option :label="index" v-for="(item,index) in 51" :value="index"></el-option>
          </el-select> 梯
          <el-select size="mini" v-model="form.household" placeholder="1" style="width:80px">
            <el-option :label="item" v-for="(item,index) in 100" :value="item"></el-option>
          </el-select> 户
        </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" size="mini" @click="saveUnit">保 存</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { request } from "../../../assets/request.js";
export default {
    data(){
        return{
            form:{
                unitName:"",   //名称
                layer:'',      //层数
                ladder:'1',     //梯
                household:'2'   //户
            },
            rules: {
            layer: [{
                pattern:/^[1-9]\d{0,2}$/,
                required: true,
                message: '层数必须是整数',
                trigger: 'blur'
            }],
            },
            formLabelWidth:'80px',
            isDial:false,
            title:"添加单元",
            unit:'',
            disabled:false
        }
    },
    created(){
        let that = this;
        this.$root.$on("addUnit", (type,data) => {
            console.log(data)
            that.disabled=false
            that.isDial = true;
            let form=that.form
            form.unitName=''
            form.layer=''
            if(type=='edit'){
                that.title="编辑单元"
                form.layer=data.totalTier
                form.unitName=data.unitNo
                let stairsType=data.stairsType
                that.disabled=true
                if(stairsType){
                    stairsType=stairsType.split('梯')
                    stairsType[1]=stairsType[1].substr(0,stairsType[1].length - 1)
                    form.ladder=stairsType[0]
                    form.household=stairsType[1]
                }
            }else{
                that.title="添加单元"
            }
            that.unit=data
        });
    },
    methods:{
        saveUnit(){
            let that=this;
            let url='houses/unit'
            let obj={
                housesId:that.unit.housesId,
                name:that.unit.name,
                totalTier:that.form.layer,
                stairsType:that.form.ladder+'-'+that.form.household,
                unit:that.unit.unit,
                unitNo:that.form.unitName,
                prefix:that.unit.prefix
            }
            console.log(obj)
            let met='POST'
            if(that.title=='编辑单元'){
                met='PUT'
                url='houses/unit/unit'
                obj={
                    id: that.unit.id,
                    stairsType: that.form.ladder+'-'+that.form.household,
                    totalTier: that.form.layer,
                }
            }
            request.ajaxPost({
                url,
                that,
                type:met,
                params:obj,
                fn(res) {
                    that.isDial = false;
                    that.$root.$emit("pedestalList");
                }
            });

        }
    }
}
</script>
<style lang="less" scoped>

</style>


